package com.atguigu.eduservice.controller;
import com.atguigu.eduservice.entity.Order;
import com.atguigu.eduservice.entity.PayResult;
import com.atguigu.eduservice.service.IPayment;
import lombok.AllArgsConstructor;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 支付
 *策略模式形成ifelse
 * @author Mr.Qu
 * @since 2021/5/18 15:30
 */
@RestController
@AllArgsConstructor
@RequestMapping("/pay/")
public class PayController {

    private final ApplicationContext applicationContext;

    /**
     * 支付API
     *
     * @param amount  金额
     * @param payType 支付类型
     * @return PayResult
     */
    @GetMapping()
    public PayResult pay(int amount, String payType) {
        Order order = new Order().setAmount(amount).setType(payType);

        //  根据【支付类型】获取对应的策略 bean
        IPayment payment = applicationContext.getBean(payType, IPayment.class);

        //  开始调用策略对应的支付业务逻辑
        return payment.pay(order);
    }
}